﻿using AdminSystem.Application.Commands.TraceabilityCommands;
using AdminSystem.Domain.Entities;
using AdminSystem.EntityFramworkCore;
using AdminSystem.Repositories.Interfaces;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Application.CommandHandlers.TraceabilityCommandHandlers
{
    public class TraceabilityAddCommandHandler : IRequestHandler<TraceabilityAddCommand, APIResult<object>>
    {
        private readonly IBaseRepositories<Traceability> traceabilityBase;
        private readonly IMapper mapper;
        public TraceabilityAddCommandHandler(IBaseRepositories<Traceability> traceabilityBase, IMapper mapper)
        {
            this.traceabilityBase = traceabilityBase;
            this.mapper = mapper;
        }
        public Task<APIResult<object>> Handle(TraceabilityAddCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<object> result = new APIResult<object>();
                result.Code = ResultCode.添加成功;
                result.Message = ResultCode.添加成功.ToString();
                var entity = mapper.Map<Traceability>(request);
                traceabilityBase.Add(entity);
               
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }
}
